/*
 * Copyright (c) 2024 坚果派
 * 
 * 公众号：nutpi
 * 电话：17752170152
 * 官网：https://www.nutpi.net/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import 'package:flutter/material.dart';
import 'package:flutter_digital_timer_depo/widgets/digital_clock.dart';
import 'package:flutter_digital_timer_depo/widgets/countdown_display.dart';
import 'package:flutter_digital_timer_depo/models/countdown_timer.dart';
import 'package:flutter_digital_timer_depo/models/time_format.dart';
import 'package:flutter_digital_timer_depo/screens/alarm_list_screen.dart';
import 'package:flutter_digital_timer_depo/widgets/copyright_info.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '数字时钟',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;

  final List<Widget> _pages = [
    // 数字时钟页面
    Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: const [
          Expanded(
            child: Center(
              child: DigitalClock(
                timeFormat: TimeFormat.chineseUppercase,
                fontSize: 48.0,
                textColor: Colors.blue,
              ),
            ),
          ),
          CopyrightInfo(),
        ],
      ),
    ),
    // 倒计时器页面
    Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Center(
              child: CountdownDisplay(
                countdownTimer: CountdownTimer(
                  startTime: DateTime.now(),
                  endTime: DateTime.now().add(const Duration(minutes: 25)),
                  label: '专注时间',
                ),
                timeFormat: TimeFormat.chineseUppercase,
                fontSize: 48.0,
              ),
            ),
          ),
          const CopyrightInfo(),
        ],
      ),
    ),
    // 闹钟页面
    Column(
      children: const [
        Expanded(
          child: AlarmListScreen(),
        ),
        CopyrightInfo(),
      ],
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('数字时钟'),
      ),
      body: _pages[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.access_time),
            label: '时钟',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.timer),
            label: '倒计时',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.alarm),
            label: '闹钟',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue,
        onTap: _onItemTapped,
      ),
    );
  }
}
